1269. 停在原地的方案数
为保证权益,题目请参考 1269. 停在原地的方案数(From LeetCode).
解决方案1
Python
python
from typing import List
class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
dp = [0] * arrLen
dp[0] = 1
t = 10 ** 9 + 7
for j in range(steps):
dpBefore = dp
dp = [0] * (j + 3)
for i in range(j + 2):
if i < arrLen:
dp[i] = dpBefore[i]
if i - 1 >= 0:
dp[i] = (dp[i] + dpBefore[i - 1]) % (t)
if i + 1 < arrLen:
dp[i] = (dp[i] + dpBefore[i + 1]) % (t)
return dp[0]
if __name__ == "__main__":
so = Solution()
print(so.numWays(2, 4))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26